package com.ddc.study.feature.feedback;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.ddc.study.R;
import com.ddc.study.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2018/8/21.
 *
 * @author lshun
 * @version 1.0
 */
public class FeedbackActivity extends BaseActivity {
    private static final int REQUEST_CODE_SELECT_IMG = 0x01;

    @BindView(R.id.edt_desc)
    EditText mEdtDesc;

    @BindView(R.id.edt_contact_way)
    EditText mContactWay;

    @BindView(R.id.cb_upload_log)
    CheckBox mCbUploadLog;

    @BindView(R.id.btn_add_picture)
    ImageButton mBtnAddPicture;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.btn_add_picture)
    void addPicture() {
        if (mUriList.size() == 3) {
            showToast("最多上传3张图片");
            return;
        }
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUEST_CODE_SELECT_IMG);
    }

    @OnClick(R.id.btn_submit)
    void submit() {
        //检查是否缺少必填项
        if (mEdtDesc.getText() == null || mEdtDesc.getText().length() == 0) {
            showToast("问题描述不能为空");
            return;
        }

        //TODO submit data
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            if (resultCode != RESULT_OK) return;
            if (data == null) return;
            final Uri imgUri = data.getData(); //获取系统返回的照片的Uri
            if (imgUri == null) return;
            if (mUriList.contains(imgUri)) {
                showToast("图片重复，请重新选择");
                return;
            }
            if (mUriList.size() == 3) {
                showToast("最多上传3张图片");
                return;
            }
            final View view = View.inflate(this, R.layout.layout_feedback_img, null);
            ((ImageView) view.findViewById(R.id.iv_img)).setImageURI(imgUri);
            ((ViewGroup) mBtnAddPicture.getParent()).addView(view, mUriList.size());
            mUriList.add(imgUri);
            view.findViewById(R.id.iv_delete).setOnClickListener(v -> {
                ((ViewGroup) view.getParent()).removeView(view);
                mUriList.remove(imgUri);
            });
        }
    }

    private List<Uri> mUriList = new ArrayList<>();
}
